package com.study.mike.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.study.mike.common.annotation.NeedDecrypt;
import com.study.mike.common.annotation.NeedEncrypt;
import com.study.mike.common.utils.SensitiveUtil;
import com.study.mike.model.dto.Test1;
import com.study.mike.model.dto.Test2;
import com.study.mike.model.dto.UserDTO;
import com.study.mike.model.entity.One;
import com.study.mike.model.entity.Sensitive;
import com.study.mike.model.entity.User;
import com.study.mike.model.query.UserQuery;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kh
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/common/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/test")
    @NeedDecrypt
    public List<User> test() {
        List<User> list = userService.queryInfo();
        List<Sensitive> sensitives = list.stream().map(e -> new Sensitive(e.getId(), e.getEmail(), e.getName())).collect(Collectors.toList());
        sensitives.forEach(item -> {
            User user = new User();
            user.setId(item.getId());
            user.setEmailSensitive(SensitiveUtil.hideEmail(item.getEmail()));
            user.setNameSensitive(SensitiveUtil.hideChineseName(item.getName()));
            userService.updateById(user);
        });
        System.out.println(sensitives);
        return list;
    }

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @GetMapping("/query")
    public R<IPage<UserDTO>> userIPage(@RequestBody @Validated UserQuery query) {
        IPage<UserDTO> userIPage = userService.pageQuery(query);
        return RUtil.success(200, "查询成功", userIPage);
    }

    @PostMapping("/queryTest")
    public String queryTest(@RequestBody @Validated UserQuery query) {
//        R<IPage<User>> pageR = this.userIPage(query);
//        System.out.println(pageR.getData().getRecords());
        return "成功了";
    }

    @GetMapping("/query1")
    public IPage<User> userIPage1(@RequestBody @Validated UserQuery query) {
//        return userService.pageQuery(query);
        return null;
    }

    /**
     * 导出用户数据
     *
     * @return
     */
    @GetMapping("/export")
    public R exportUser() throws IOException {
        userService.exportUser();
        return RUtil.success("操作成功");
    }

    /**
     * easyExcel导出数据
     *
     * @return
     */
    @PostMapping("/exportUserInfo")
    public R exportUserInfo() {
        userService.exportUserInfo();
        return RUtil.success();
    }

    /**
     * 导入用户数据
     *
     * @return
     */
    @GetMapping("/import")
    public R importUser() throws Exception {
        userService.importUser();
        return RUtil.success("操作成功");
    }

    @GetMapping("/one")
    public void one(One one) {
        userService.one(one);
    }

    @GetMapping("/one1")
    public void onb() {
        Test1 list1 = new Test1();
        list1.setAge("123");
        list1.setOne("第一个");
        Test1 list2 = new Test1();
        list2.setAge("123");
        list2.setOne("第二个");
        List<Test1> test1 = new ArrayList<>();
        test1.add(list1);
        test1.add(list2);
        List<Test2> test2s = test1.stream().map(e -> new Test2(e.getOne())).collect(Collectors.toList());
        System.out.println(test2s + "数据");

    }

    @GetMapping("/add")
    public void one(@RequestBody @Validated User one) {
        userService.add(one);
    }

    @GetMapping("/getUserInfoByRedis")
    public List<User> getUserInfoByRedis() {
        return userService.getUserInfoByRedis();
    }

    @GetMapping("/headInfo")
    public void headInfo(@RequestBody User user, HttpServletRequest request) {
        System.out.println("请求头" + request.getHeader("userName"));

    }

    @GetMapping("/template")
    public void template() {
        redisTemplate.opsForValue().set("User:query:userName", "james");
    }

    @GetMapping("/encryptList")
    @NeedEncrypt
    public void encryptList() {
        userService.encryptName();
    }


}

